<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" placeholder="请输入用户名" name="username" id="names"><br>
    <input type="text" placeholder="请输入年龄" name="age" id="age"><br>
    <input type="text" placeholder="请输入性别" name="gender" id="gender">
    <button type="submit" onclick="setinfo()">提交</button>



    <script>
        const setinfo = () => {
            name = document.getElementById("names")
            age = document.getElementById("age")
            gender = document.getElementById("gender")

            person = {
                name: names.value,
                age: age.value,
                gender: gender.value
            }
            submitInfo(person)
        }


        const submitInfo = (person) => {
            const xhr = new XMLHttpRequest()
            xhr.open('post', '/addStudent', true)
            xhr.send(JSON.stringify(person))
            if (xhr.readyState === 4 && xhr.status === 200) {
                console.log(xhr.responseText)
            }
        }

        //查询信息
       const loadInfo=()=>{
        const xhr = new XMLHttpRequest()
        xhr.open('get','/getStudent',true)
        xhr.send(null)
        xhr.onreadystatechange=function(){
            if (xhr.readyState === 4 && xhr.status === 200) {
                console.log(xhr.responseText)
                let studentList=JSON.parse(xhr.responseText)
                console.log(studentList)
                showInfo(studentList)
            }
        }
    }
    const showInfo=(studentList)=>{
        let tbody = document.getElementById('studentList')
        tbody.innerHTML=''
        for(let i =0;i<studentList.length;i++){
            let stu = JSON.parse(studentList[i])
            console.log(stu)
            tbody.innerHTML=tbody.innerHTML+'<tr><td>'+stu.name+'</td><td>'+stu.gender+'</td><td>'+stu.age+'</td></tr>'
        }
    }

    </script>
</body>

</html>